var viewerData;

// user clicked on one of the NARCIS person entries
function personClicked(node)
{
	var daistr  = node.lastChild.innerHTML;
	var regex   = new RegExp('^DAI: (info:eu-repo/dai/nl/[0-9]+X?)$');
	var matches =  daistr.match(regex);
	if (matches)
	{
		var newdai = matches[1];
		input  = document.getElementById('daiInput').value = newdai;
	}
	return false;
}

// user clicked on Help
function helpClicked()
{
	var helpnode  = document.getElementById('docs');
	if ( helpnode.clientHeight==0 )
	{
		helpnode.style.height = 'auto';
	}
	else
	{
		helpnode.style.height = '0pt';
	}
	gadgets.window.adjustHeight();
	return false;
}

// simple mod11 (ISBN-like) check function
function checkMod11(num)
{
	if (typeof num == "number") { num = num.toString() };
	if (num.length>10) { return false };

	sum=0;
	for ( i=num.length-1, j=1; i>=0; i--, j++ )
	{
		d = num.charCodeAt(i) - 48;
		if ( d==88-48 || d==120-48 ) { d = 10 }; // Handle 'X' and 'x'
		if ( d < 0 || d>10 ) { return false };
		sum+=j*d;
	}
	return ( (sum%11)==0 );
}

// validate the DAI
function validateDAI(dai)
{
	// see http://wiki.surffoundation.nl/display/standards/DAI#DAI-Syntax
	if (dai.length!=29 && dai.length!=30) { return false };
	if (dai.substr(0,20) != 'info:eu-repo/dai/nl/') { return false };
	dainum = dai.substring(20);
	if (!checkMod11(dainum)) { return false };
	return dai;
}

// will be called by the OS container after the dai is updated
function osapiCallback(data,reload)
{
	if (data.error) { throw new Exception(data.error.message); }
	location.reload();
}

// called when the user presses "reset dai" button
function resetDAI()
{
	// remove DAI from appData;  callback will reload iframe
	osapi.appdata.delete({userId: '@me', appId: 'MRP_DAI'}).execute(osapiCallback);
	return false;
}

// called when the user presses "set dai" button
function setDAI()
{
	dai = document.getElementById('daiInput').value;
	if (dai.length==9 || dai.length==10)
	{
		dai = 'info:eu-repo/dai/nl/'+ dai;
	}
	if ( !(dai = validateDAI(dai)) ) 
	{
		alert('Invalid DAI!  Must be of the form "info:eu-repo/dai/nl/12309884X"');
		return false;
	}
	// update appData;  callback will reload iframe
	osapi.appdata.update({userId: '@me', appId: 'MRP_DAI', data: {dai: dai}}).execute(osapiCallback);
	return false;
}

// will be called by the OS container after a dai is requested
function requestDaiCallback(data,reload)
{
	if (data.error) { throw new Exception(data.error.message); }

	today = new Date();
	daiReqDate = today.getDate() + '-' + (today.getMonth()+1) + '-' + today.getFullYear();
	osapi.appdata.update(
		{userId: '@me', appId: 'getdai_gadget', data: {daiReqDate: daiReqDate}}
	).execute(osapiCallback);

	return false;
}

// called when user presses "request dai" button
function showRequestDAI()
{
	var node = document.getElementById('requestAck');
	node.style.display = "block";
	gadgets.window.adjustHeight();
}

// called when user submits dai request
function requestDAI()
{
	var postData = viewerData;
	osapi.http.post({
		"href"   : "https://mfsp.gadgets.surfconext.nl/getdai/requestdai.php",
		"body"   : postData,
		"format" : "text",
		"authz"  : "signed"
	}).execute(requestDaiCallback);

	return false;
}

// called when user pressed "reset dai" button
function resetDAIRequest()
{
	osapi.appdata.delete({userId: '@me', appId: 'getdai_gadget'}).execute(osapiCallback);
	return false;
}


function onLoad(daiRequestDate,osData)
{
	viewerData = osData;
	gadgets.window.adjustHeight();
}
